
package com.yimixian.app.ui;

import com.viewpagerindicator.CirclePageIndicator;
import com.yimixian.app.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class GuidelinesView extends FrameLayout {

    public interface Listener {
        void onCompleteButtonClicked();
    }

    @InjectView(R.id.view_pager)
    ViewPager mViewPager;
    @InjectView(R.id.indicator_view)
    CirclePageIndicator mPageIndicator;

    private Listener mListener;

    public GuidelinesView(Context context) {
        super(context);
        initGuidelinesView();
    }

    public GuidelinesView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initGuidelinesView();
    }

    public GuidelinesView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initGuidelinesView();
    }

    private void initGuidelinesView() {
        LayoutInflater.from(getContext()).inflate(R.layout.guidelines_view, this);
        ButterKnife.inject(this);
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return 4;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.guideline_page_view,
                        container, false);
                ImageView introImage = (ImageView) view.findViewById(R.id.intro_img);
                view.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
                int introResId = getResources().getIdentifier(
                        "app_intro" + (position + 1), "drawable", getContext().getPackageName());
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.RGB_565;
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), introResId, options);
                introImage.setImageBitmap(bitmap);
                if (position == 3) {
                    view.setOnTouchListener(new OnTouchListener() {
                        float initX = 0;

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            switch (event.getAction()) {
                                case (MotionEvent.ACTION_DOWN):
                                    initX = event.getX();
                                    break;
                                case (MotionEvent.ACTION_CANCEL):
                                case (MotionEvent.ACTION_UP):
                                    if (!(event.getX() > initX)) {
                                        if (mListener != null) {
                                            mListener.onCompleteButtonClicked();
                                        }
                                    }
                            }
                            return false;
                        }
                    });
                    view.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mListener != null) {
                                mListener.onCompleteButtonClicked();
                            }
                        }
                    });
                }
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        mPageIndicator.setViewPager(mViewPager);
        mPageIndicator.setStrokeColor(getResources().getColor(R.color.white));
        mPageIndicator.setPageColor(getResources().getColor(R.color.white));
        mPageIndicator.setFillColor(getResources().getColor(R.color.ymx_orange));
     }

    public void setListener(Listener listener) {
        mListener = listener;
    }
}
